package com.common.android.utils.smiley;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nd.android.u.allcommon.R;

public class SmileyAdapter extends BaseAdapter {
    private final String TAG = "SmileyAdapter";
    private Context mContext;
    private List<Smiley> mSmileyList = null;
    private Smileyhelper smileyhelper;
    private int pageSize = 21;
    private int pageNo = 0;
    private int total = 0;
    private int mType;
    private int mLayoutId;

    public SmileyAdapter(Context context, int type) {
        this.mContext = context;
        smileyhelper = Smileyhelper.getInstance();
        mType = type;
        initSize();
        initAllSmileyList();
    }

    private void initSize() {
        if (mType == Smileyhelper.CAT || mType == Smileyhelper.HISTORY
        		|| mType == Smileyhelper.COS) {
            pageSize = Smileyhelper.SIZE_CAT;
            mLayoutId = R.layout.smiley_item_cat;
        } else {
            pageSize = Smileyhelper.SIZE_OTHER;
            mLayoutId = R.layout.smiley_item;
        }
    }

    public void setSmileyList(List<Smiley> smileyList) {
        mSmileyList = smileyList;
    }

    private void initAllSmileyList() {
        mSmileyList = smileyhelper.getSmileyList(mType);
        if (mSmileyList != null) {
            total = mSmileyList.size();
        }
    }

    @Override
    public int getCount() {
        if (mSmileyList == null)
            return 0;
        return (pageNo + 1) * pageSize > total ? total - (pageNo) * pageSize : pageSize;
    }

    public int getPageNoCount() {
        return (int) total / pageSize + (total % pageSize == 0 ? 0 : 1);
    }

    @Override
    public Object getItem(int position) {
        if (mSmileyList == null)
            return null;
        else
            return mSmileyList.get(pageNo * pageSize + position);
    }

    @Override
    public long getItemId(int position) {
        return pageNo * pageSize + position;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    class Holder {
        ImageView image;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        Smiley smiley = mSmileyList.get(pageNo * pageSize + position);
        /*
         * if (Smileyhelper.getInstance().isCatID(smiley.id)){ mLayoutId=R.layout.smiley_item_cat;
         * }else{ mLayoutId=R.layout.smiley_item; }
         */
        if (convertView == null) {
            holder = new Holder();
            LayoutInflater inflate = LayoutInflater.from(mContext);
            convertView = inflate.inflate(mLayoutId, null);
            if(mType == Smileyhelper.HISTORY){
            	if (Smileyhelper.getInstance().isCatID(smiley.id) 
                         || Smileyhelper.getInstance().isCosID(smiley.id)) {
                     convertView.findViewById(R.id.smiley_img).setVisibility(View.VISIBLE);
                     convertView.findViewById(R.id.smiley_img_normal).setVisibility(View.GONE);
                     holder.image = (ImageView) convertView.findViewById(R.id.smiley_img);
                 } else {
                     convertView.findViewById(R.id.smiley_img).setVisibility(View.GONE);
                     convertView.findViewById(R.id.smiley_img_normal).setVisibility(View.VISIBLE);
                     holder.image = (ImageView) convertView.findViewById(R.id.smiley_img_normal);
                 }
            }else{
                holder.image = (ImageView) convertView.findViewById(R.id.smiley_img);
            }
            convertView.setTag(holder);
        }
        holder = (Holder) convertView.getTag();
        smileyhelper.setSmiley(mContext, holder.image, smiley.name);
        return convertView;
    }
}
